﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Dynamic.Core;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using PMS.XCMG.Entity;
using PMS.XCMG.IBusiness;
using PMS.XCMG.Util;
using PMS.XCMG.Models;

namespace PMS.XCMG.Business
{
    public class RealIncomeBusiness : BusRepository<RealIncome>, IRealIncomeBusiness, IScopedDependency
    {
        private IServiceProvider SvcProvider { get; set; }
        public RealIncomeBusiness(GDbContext context, IServiceProvider svcProvider)
            : base(context)
        {
            this.SvcProvider = svcProvider;
        }

        public async Task<PageResult<RealIncome>> GetPageResultAsync(PageInput<RealIncomeQM> query)
        {
            try
            {
                var queryable = this.GetQueryable(true);
                queryable = queryable.Include(i => i.Precollected).ThenInclude(i => i.PlanInfor);

                var search = query.Search;

                var result = await this.GetPageResultAsync(queryable, query);
                if (!search.Id.IsNullOrEmpty())
                {
                    if (!result.Data.Any(w => w.Id == search.Id))
                    {
                        var idOne = await this.GetQueryable(true).SingleOrDefaultAsync(w => w.Id == search.Id);
                        result.Data.Add(idOne);
                        result.Total += 1;
                    }
                }
                return result;
            }
            catch(Exception ex)
            {
            }

            return new PageResult<RealIncome>();
        }
    }
}
